/**
 * @type type: 1-团长 2-副团长
 * @status 门店状态(0.申请中，1.运营中，2.休店中，98.禁用，99.已拒绝)
 */
var util = require("../../utils/util.js");
var api = require("../../config/api.js");
var app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        type: 1,
        title: "",
        leaderInfo: {},
        sellData: {},
        addressVo: {},
        rewardInfo:null,
        timeList:null,
        manageList: [
            {
                title: "订单中心",
                imageUrl:
                    "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/Leader/center/1.png",
                isLeader: false,
                url: "/Leader/manage/my-pickup/my-pickup"
            },
            {
                title: "用户详情",
                imageUrl:
                    "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/Leader/center/2.png",
                isLeader: false,
                url: "/Leader/manage/my-group/my-group"
            },
            {
                title: "分享赚钱",
                imageUrl:
                    "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/Leader/center/3.png",
                isLeader: false,
                url: "/Leader/products/products"
            },
            {
                title: "副团长管理",
                imageUrl:
                    "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/Leader/center/4.png",
                isLeader: true,
                url: "/Leader/manage/my-deputy/my-deputy"
            },
            {
                title: "申请休店",
                imageUrl:
                    "http://booking-prod.oss-cn-shenzhen.aliyuncs.com/tata/Leader/center/5.png",
                isLeader: true,
                url: "/Leader/status/status"
            },
            {
                title: "收益详情",
                imageUrl:
                    "https://tataposter.zy100hui.com/202107194ad1ce5a-d6cf-402d-bf8a-ce23a429f770.png",
                isLeader: true,
                url: "/Leader/pkiCenter/pkiCenter"
            },
        ],
    },
    // 倒计时
    countDown: function (arrList) {
        var that = this;
        var list = this.data.timeList
        arrList.forEach((item,index)=>{
            let timer = `timeList[`+index+`].timer`
            let day = `timeList[`+index+`].day`
            let hour = `timeList[`+index+`].hour`
            let minute = `timeList[`+index+`].minute`
            let second = `timeList[`+index+`].second`
            let isOver = `timeList[`+index+`].isOver`
            that.setData({
                [timer]: setInterval(function () { //周期计时器，每隔1秒执行一次方法里的代码
                    //得到一个从现在时间开始到活动结束的时间戳
                    var downTime = parseInt(new Date(item.endTime.replace(/-/g, "/")).getTime() - new Date().getTime())
                    // 倒计时结束
                    if (downTime <= 0) {
                        that.setData({
                            [day]: '00',
                            [hour]: '00',
                            [minute]: '00',
                            [second]: '00',
                            [isOver]:true
                        })
                        //结束周期计时器
                        clearInterval(that.data.timeList[index].timer);
                        return;
                    }
                    //计算距离活动还有多少天、时、分、秒
                    var d = parseInt(downTime / 1000 / 3600 / 24);
                    var h = parseInt(downTime / 1000 / 3600 % 24);
                    var m = parseInt(downTime / 1000 / 60 % 60);
                    var s = parseInt(downTime / 1000 % 60);
                    //统一格式的显示
                    d < 10 ? d = '0' + d : d;
                    h < 10 ? h = '0' + h : h;
                    m < 10 ? m = '0' + m : m;
                    s < 10 ? s = '0' + s : s;
                    //同步显示
                    that.setData({
                        [day]: d,
                        [hour]: h,
                        [minute]: m,
                        [second]: s
                    })
                }, 1000)
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let manageList = this.data.manageList;
        if(options.rs == 2){
            // 副团长
            manageList = manageList.filter(item => !item.isLeader)
        }
        this.setData({
            navH: app.globalData.navHeight,
            type: options.rs,
            manageList
        });
        wx.setNavigationBarTitle({
            title: this.data.title,
        });

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getMyLeaderInfo();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (ops) {
        let that = this
        that.shareCount(that.data.leaderInfo.leaderId,ops.target.dataset.id)
        let url = '/pages/index/index'
        let title= '108元福利放送中，仅限今天！'
        let imageUrl = '/static/images/share.jpeg'
        if (ops.from === 'button') {
            // 来自页面内转发按钮
            if(ops.target.dataset.type == 'ucenter'){
                imageUrl='https://tataposter.zy100hui.com/20210805948955a3-961f-477b-a834-f4d25647490b.jpg'
                title= '快来加入獭獭团长，和我一起赚钱吧～'
                url='/pages/ucenter/index/index?leaderId='+that.data.leaderInfo.leaderId+'&activityId='+ops.target.dataset.id
            }
        }
        //console.log(url,title,imageUrl)
        return {
            title:title,
            imageUrl:imageUrl,
            path:url //点击分享的图片进到哪一个页面
        }
    },
    shareCount(lenaderId,activityId) {
        let that = this
        util.request(api.leader.shareCount+'?leaderId='+lenaderId+'&activityId='+activityId, {}, "POST")
            .then((res) => {
                wx.hideLoading();
                wx.showToast({
                  title: '分享成功！',
                })
            })
            .catch((err) => {
                wx.hideLoading();
            });
    },
    getRewardInfo(lenaderId) {
        let that = this
        wx.showLoading({
            title: "正在加载",
        });
        util.request(api.leader.activityCenter+'?leaderId='+lenaderId, {}, "POST")
            .then((res) => {
                wx.hideLoading();
                that.setData({
                    rewardInfo:res.result
                })
                this.countDown(res.result)
            })
            .catch((err) => {
                wx.hideLoading();
            });
    },
    getMyLeaderInfo() {
        let that = this
        wx.showLoading({
            title: "正在加载",
        });
        util.request(api.leader.getMyLeaderInfoUrl, {}, "POST")
            .then((res) => {
                wx.hideLoading();
                that.getRewardInfo(res.result.leaderId);
                const sellData = res.result.sellData;
                // const
                this.setData({
                    leaderInfo: res.result,
                    sellData: res.result.sellData,
                    addressVo: res.result.addressVo
                });
                wx.setStorageSync("addressVo", res.result.addressVo);
            })
            .catch((err) => {
                wx.hideLoading();
            });
    },
    goTo(e){
        let { url, title } = e.currentTarget.dataset;
        if(title === "申请休店"){
            if(!this.data.addressVo.id){
                return wx.showToast({
                    title: "请选择提货地址",
                    icon: "none"
                })
            }
            switch(this.data.addressVo.status){
                case 0: //申请中
                    url = "/Leader/application/application-result/application-result?rs=0"
                    break;
                case 99: // 已拒绝
                    url = "/Leader/application/application-result/application-result?rs=1"
                    break;
                case 2: // 休店中
                    url += "?id=" + this.data.addressVo.id + "&rs=1"
                    break;
                case 1: //运营中
                    url += "?id=" + this.data.addressVo.id + "&rs=0"
            }
        }
        wx.navigateTo({
            url
        })
    }
});
